package com.ruoyi.legal.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.legal.domain.Message;
import com.ruoyi.legal.service.IMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息提醒Controller
 *
 * @author xwj
 * @date 2025-06-06
 */
@RestController
@RequestMapping("/legal/message")
@Api(tags = "消息提醒")
public class MessageController extends BaseController {
    @Autowired
    private IMessageService messageService;

    @PostMapping("/unreadNum")
    @ApiOperation("未读数")
    public ResponseModel unreadNum(@RequestBody Message message) {
        Long userId = getUserId();
        if(userId != 1) {
            message.setUserId(userId);
        }

        int num = messageService.unreadNum(message);
        return ResponseModel.of(num);
    }

    /**
     * 查询消息提醒列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody Message message) {
        startPage();
        Long userId = getUserId();
        if(userId != 1) {
            message.setUserId(userId);
        }
        List<Message> list = messageService.selectMessageList(message);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody Message message) {
        Long userId = getUserId();
        if(userId != 1) {
            message.setUserId(userId);
        }
        startOrderBy();
        List<Message> list = messageService.selectMessageList(message);
        return success(list);
    }

    /**
     * 导出消息提醒列表
     */
    @Log(title = "消息提醒", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, Message message) {
        List<Message> list = messageService.selectMessageList(message);
        ExcelUtil<Message> util = new ExcelUtil<Message>(Message.class);
        util.exportExcel(response, list, "消息提醒数据");
    }

    /**
     * 获取消息提醒详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id) {
        return success(messageService.selectMessageById(id));
    }

    /**
     * 新增消息提醒
     */
    @Log(title = "消息提醒", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody Message message) {
        SysUser user = getLoginUser().getUser();
        if (message.getId() == null) {
            message.setCreateId(user.getUserId());
            message.setCreateTime(DateUtils.getNowDate());
            message.setCreateBy(user.getNickName());
            messageService.insertMessage(message);
        } else {
            message.setUpdateId(user.getUserId());
            message.setUpdateTime(DateUtils.getNowDate());
            message.setUpdateBy(user.getNickName());
            messageService.updateMessage(message);
        }

        return success(message);
    }

    /**
     * 修改消息提醒
     */
    @Log(title = "消息提醒", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody Message message) {
        SysUser user = getLoginUser().getUser();
        message.setUpdateId(user.getUserId());
        message.setUpdateTime(DateUtils.getNowDate());
        message.setUpdateBy(user.getNickName());
        return success(messageService.updateMessage(message));
    }

    /**
     * 删除消息提醒
     */
    @Log(title = "消息提醒", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(messageService.deleteMessageByIds(ids));
    }

    @Log(title = "消息提醒", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody Message message) {
        int num = messageService.delete(message);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<Message>> queryPage(@RequestBody Message message) {
        Pages<Message> page = messageService.queryPage(message);
        return ResponseModel.of(page);
    }

    @GetMapping("/messageGenerated")
    @ApiOperation("生成消息")
    public ResponseModel messageGenerated() {
        messageService.messageGenerated();
        return ResponseModel.ok();
    }
}
